package com.avocado.configuration.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.avocado.configuration.dto.IdGeneratorAddDto;
import com.avocado.configuration.dto.IdGeneratorEditDto;
import com.avocado.configuration.repository.IdGeneratorRepository;

import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;

import com.avocado.configuration.dto.IdGeneratorQueryDto;
import com.avocado.configuration.entity.IdGenerator;
import com.avocado.configuration.service.IdGeneratorService;

@Service
public class IdGeneratorServiceImpl implements IdGeneratorService {
	

	private final IdGeneratorRepository idGeneratorRepository;

    public IdGeneratorServiceImpl(IdGeneratorRepository idGeneratorRepository) {
        this.idGeneratorRepository = idGeneratorRepository;
    }

    @Override
	public IdGenerator getOne(Long id) {

		return idGeneratorRepository.getOne(id);
	}
	@Override
    public Long  add(IdGeneratorAddDto dto) {

		IdGenerator idGenerator=new IdGenerator();

		BeanUtils.copyProperties(dto,idGenerator);

		idGenerator.setGenName(idGenerator.getGenName().toUpperCase());
    	
    	idGeneratorRepository.saveAndFlush(idGenerator);
		
		return idGenerator.getId();
	}
    
	@Override
    public void update(IdGeneratorEditDto dto) {

    	IdGenerator old=idGeneratorRepository.getOne(dto.getId());

		BeanUtils.copyProperties(dto,old);
    	
    	idGeneratorRepository.saveAndFlush(old);

	}
	@Override
    public  Page<IdGenerator> findByPage(Pageable pageable,IdGeneratorQueryDto dto) {
    	  

     	 return idGeneratorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {

         List<Predicate> predicates = new ArrayList<>();

         if(!StringUtils.isEmpty(dto.getGenName())){
             predicates.add(criteriaBuilder.like(root.get("genName").as(String.class),"%".concat(dto.getGenName().toUpperCase()).concat("%")));
         }

         Predicate[] predicateArray = new Predicate[predicates.size()];

         return criteriaBuilder.and(predicates.toArray(predicateArray));
     }, pageable);
     	 

    }
    
    
	@Override
    public List<IdGenerator> findAll(IdGeneratorQueryDto dto) {
   	
    	return idGeneratorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {

        List<Predicate> predicates = new ArrayList<>();

        if(!StringUtils.isEmpty(dto.getGenName())){
            predicates.add(criteriaBuilder.like(root.get("genName").as(String.class),"%".concat(dto.getGenName()).concat("%")));
        }

        Predicate[] predicateArray = new Predicate[predicates.size()];

        return criteriaBuilder.and(predicates.toArray(predicateArray));
    });

   }
	
	@Override
    public Boolean deleteById(Long id) {
		
		IdGenerator old=idGeneratorRepository.getOne(id);

		if(old.getId()==1L) {
    	   return Boolean.FALSE;
    	}
		
		idGeneratorRepository.delete(old);
		
		return Boolean.TRUE;
	}


}
